package com.sc.test;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.sc.test.db.DBManager;
import com.sc.test.db.User;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Shen Chao on 2018-01-11-011 上午 10:01.
 */

public class GreenDaoActivity extends AppCompatActivity {
    @BindView(R.id.et_greendao_name)
    TextView etGreendaoName;
    @BindView(R.id.et_greendao_psw)
    TextView etGreendaoPsw;
    @BindView(R.id.et_greendao_address)
    TextView etGreendaoAddress;
    @BindView(R.id.lv_user_list)
    ListView lvUserList;
    private DBManager dbManager;
    private Long id;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_greendao);
        ButterKnife.bind(this);
        dbManager = DBManager.getInstance(this);
    }

    @OnClick({R.id.btn_greendao_add, R.id.btn_greendao_delete, R.id.btn_second_update,
            R.id.btn_second_findall})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_greendao_add:
                String name = etGreendaoName.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    Toast.makeText(this, "name不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                String psw = etGreendaoPsw.getText().toString();
                if (TextUtils.isEmpty(psw)) {
                    Toast.makeText(this, "psw不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                String address = etGreendaoAddress.getText().toString();
                if (TextUtils.isEmpty(address)) {
                    Toast.makeText(this, "name不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                User u = new User(name, psw, address);
                if (dbManager.add(u)) {
                    Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
                    etGreendaoName.setText("");
                    etGreendaoPsw.setText("");
                    etGreendaoAddress.setText("");
                } else {
                    return;
                }
                break;
            case R.id.btn_greendao_delete:
                User ud = new User();
                ud.setId(id);
                dbManager.delete(ud);
                setlistView(this);
                break;
            case R.id.btn_second_update:
                String name1 = etGreendaoName.getText().toString();
                User u1 = new User();
                if (id != null) {
                    u1.setId(id);
                } else if (id == null && TextUtils.isEmpty(name1)) {
                    return;
                } else if (id == null && !TextUtils.isEmpty(name1)) {
                    User userResult = dbManager.findByName(name1);
                    u1.setId(userResult.getId());
                }
                String psw1 = etGreendaoPsw.getText().toString();
                u1.setName(name1);
                u1.setPsw(psw1);
                dbManager.updateUser(u1);
                setlistView(this);
                break;
            case R.id.btn_second_findall:
                setlistView(this);
                break;
        }
    }

    private void setlistView(final Context context) {
        final List<User> users = dbManager.findAll();
        lvUserList.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return users.size();
            }

            @Override
            public Object getItem(int i) {
                return null;
            }

            @Override
            public long getItemId(int i) {
                return 0;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                View v = LayoutInflater.from(context).inflate(R.layout.activity_user_list_item, null);
                TextView tv_user_id = (TextView) v.findViewById(R.id.tv_user_list_item_id);
                TextView tv_user_name = (TextView) v.findViewById(R.id.tv_user_list_item_name);
                TextView tv_user_psw = (TextView) v.findViewById(R.id.tv_user_list_item_psw);
                tv_user_id.setText(users.get(i).getId().toString());
                tv_user_name.setText(users.get(i).getName());
                tv_user_psw.setText(users.get(i).getPsw());
                return v;
            }
        });
        lvUserList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                etGreendaoName.setText(users.get(i).getName());
                etGreendaoPsw.setText(users.get(i).getPsw());
                id = users.get(i).getId();
            }
        });
    }
}
